package 算法.leetcode.labuladong.二叉树;

import 算法.leetcode.offer.base.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author lchenglong
 * @date 2022/6/24
 */
public class 从根到叶的二进制之和  {
    static class Solution {
Queue< Integer> queuq = new LinkedList<>();
        public static void main(String[] args) {
          TreeNode root = TreeNode.init(new ArrayList<>(Arrays.asList(1,0,1,0,1,0,1)));
          Solution solution = new Solution();
            int ans = solution.sumRootToLeaf(root);
            System.out.println(ans);
        }
        int ans = 0;
        int sum = 0;
        public int sumRootToLeaf(TreeNode root) {
            travel(root);
            return ans;
        }

        public void travel(TreeNode root){
            if (root == null)return;
            if (root.left == null && root.right == null){
                // 到达叶子节点,计算总数
                System.out.println(111);
                ans += sum<<1|root.val;
            }
            // 前序位置
            sum = sum<<1|root.val;
            travel(root.left);
            travel(root.right);

            sum=sum >> 1;
        }
    }
}
